package 面向对象_上.k构造器.练习2;

/* 3.修改上题中类和构造器，增加 name 属性,
 *   使得每次创建 Person 对象的同时初始化对象的 age 属性值和 name 属性值。
 */

public class PersonTest {

    public static void main(String[] args) {

        Person p2 = new Person("Tom",21);

        System.out.println("name = " + p2.getName() + ",age = " + p2.getAge());
    }
}

class Person {

    private int age;
    private String name;

    public Person(){
        age = 18;
    }

    public Person(String n,int a){
        name = n;
        age = a;
    }

    public void setName(String n){
        name = n;
    }

    public String getName(){
        return name;
    }

    public void setAge(int a){
        if(a < 0 || a > 130){
//			throw new RuntimeException("传入的数据据非法");
            System.out.println("传入的数据据非法");
            return;
        }

        age = a;

    }

    public int getAge(){
        return age;
    }
}
